package com.ms.qw.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;

/**
 * User: Matrix
 * Date: 26/01/13
 */
public class NumberUtil {


    public static DecimalFormat buildDecimalFormat() {
        DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(new Locale("es"));
        symbols.setDecimalSeparator('.');
        symbols.setGroupingSeparator(',');
        return new DecimalFormat("#,###,###,##0.00", symbols);
    }

    public static BigDecimal nvlIsZero(BigDecimal value1) {
        if (value1 == null) {
            return BigDecimal.ZERO;
        }
        return value1;
    }

    public static Integer nvlIsZero(Integer value1) {
        if (value1 == null) {
            return 0;
        }
        return value1;
    }

    public static String formatNumber(double pValue) {
        java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(",##0.0");
        java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        symbols.setDecimalSeparator('.');
        formatDecimal.setDecimalFormatSymbols(symbols);
        return formatDecimal.format(pValue).toString();
    }


    public static double getDecimalNumberRedondeado(double pDecimal) {
        return NumberUtil.getDecimalNumber(NumberUtil.formatNumber(pDecimal));
    }


    public static void main(String[] args) {
        System.out.println(getRedondeoEspecial( 0/1000));
    }
    public static double getRedondeoEspecial(double montoTotal) {


        double vRedondearTo = 0.5;

        double montoDecimal = NumberUtil.trunc((NumberUtil.getDecimalNumberRedondeado(montoTotal - NumberUtil.trunc(montoTotal)) * 10));
        montoDecimal /= 10;

        double montoEntero = NumberUtil.trunc(montoTotal);

        if(montoDecimal ==0 || montoDecimal ==vRedondearTo){
            if(montoEntero==0){
                return montoDecimal;
            }
            return montoEntero+montoDecimal;
        }



        String montoReal = NumberUtil.formatNumber(montoTotal);


        double montoFinal = 0.0;

        if (montoDecimal > vRedondearTo)
            montoFinal = montoEntero + vRedondearTo * 2;
        else
            montoFinal = montoEntero + vRedondearTo;


        return montoFinal;
    }

    public static int trunc(double pDato) {
        String dato = String.valueOf(pDato);
        if (dato.lastIndexOf(".") != -1)
            return Integer.parseInt(dato.substring(0, dato.lastIndexOf(".")));
        else
            return Integer.parseInt(dato);
    }


    public static double getDecimalNumber(String pDecimal) {
        double decimalNumber = 0.00;
        try {
            java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat("###,###,##0.0");
            java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
            symbols.setGroupingSeparator(',');
            symbols.setDecimalSeparator('.');
            formatDecimal.setDecimalFormatSymbols(symbols);
            decimalNumber = formatDecimal.parse(pDecimal).doubleValue();
        } catch (ParseException errParse) {
            errParse.printStackTrace();
        }
        return decimalNumber;
    }

    public static String formatNumber(double pValue, int pDecimales) {
        java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(",##0." + caracterIzquierda("", pDecimales, "0"));
        java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        symbols.setDecimalSeparator('.');
        formatDecimal.setDecimalFormatSymbols(symbols);
        return formatDecimal.format(pValue).toString();
    }

    public static String caracterIzquierda(String parmString, int parmLen, String parmCaracter) {

        String tempString = parmString;

        if (tempString.length() > parmLen)
            tempString = tempString.substring(tempString.length() - parmLen,
                    tempString.length());
        else {
            while (tempString.length() < parmLen)
                tempString = parmCaracter + tempString;
        }

        return tempString;

    }
}
